#include "tcomboxdelegate.h"
#include <QComboBox>

TComBoxDelegate::TComBoxDelegate(QObject *parent)
    : QStyledItemDelegate{parent}
{}

void TComBoxDelegate::setItem(QStringList items, bool editable)
{
    m_itemList = items;
    m_editable = editable;
}

QWidget *TComBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);

    QComboBox *editor=  new QComboBox(parent);
    editor->setEditable(m_editable);
    for(int i = 0; i < m_itemList.size(); ++i)
    {
        editor->addItem(m_itemList.at(i));
    }
    return editor;
}

void TComBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString str = index.model()->data(index, Qt::EditRole).toString();
    QComboBox *combBox = static_cast<QComboBox*>(editor);
    combBox->setCurrentText(str);
}

void TComBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox *combBox = static_cast<QComboBox*>(editor);
    QString str = combBox->currentText();
    model->setData(index, str, Qt::EditRole);
}

void TComBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(index);
    editor->setGeometry(option.rect);
}



